home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Includes / UBehavior.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  9.8 KB  |  285 lines  |  [TEXT/MPS ]

  1. // UBehavior.h
  2. // Copyright © 1991-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4.  
  5. #ifndef __UBEHAVIOR__
  6. #define __UBEHAVIOR__
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UGEOMETRY__
  11. #include "UGeometry.h"
  12. #endif
  13.  
  14. #ifndef __UOBJECT__
  15. #include "UObject.h"
  16. #endif
  17.  
  18. // just for IdlePhase
  19. #ifndef __UEVENTHANDLER__
  20. #include "UEventHandler.h"
  21. #endif
  22.  
  23. class MScriptableObject;
  24. class TAppleEvent;
  25. class TDocument;
  26. class TEventHandler;
  27.  
  28. //----------------------------------------------------------------------------------------
  29. // TBehavior
  30. //----------------------------------------------------------------------------------------
  31.  
  32. class TBehavior : public TObject
  33. {
  34.     MA_DECLARE_CLASS;
  35.     
  36. public:
  37.     TBehavior();
  38.         // Constructor
  39.  
  40.     //------------------------------------------------------------------------------------
  41.     // Initializer and I<Method>.
  42.     //------------------------------------------------------------------------------------
  43.  
  44.     void IBehavior(IDType itsIdentifier);
  45.         // Initialization method
  46.  
  47.     virtual TObject* Clone();            // OVERRIDE
  48.         // We have overridden this to do a deep clone of the behavior
  49.         
  50.     virtual ~TBehavior();
  51.         // Pass the Free message on to all behaviors in the chain before calling
  52.         // Inherited::Free
  53.  
  54.     //------------------------------------------------------------------------------------
  55.     // Idling.
  56.     //------------------------------------------------------------------------------------
  57.  
  58.     virtual void SetIdleFreq(long newIdleFreq);
  59.         // Call to set the handler's idling frequency.
  60.  
  61.     virtual long NextIdle();
  62.         // Returns the number Tickcount for when the handler should next be idled.
  63.  
  64.     virtual void HandleIdle(IdlePhase phase);
  65.         // Calls DoIdle if the handler is eligible.
  66.  
  67.     virtual Boolean DoIdle(IdlePhase phase);
  68.         // Returns true if it frees itself so caller can know
  69.  
  70.     //------------------------------------------------------------------------------------
  71.     // Stream I/O protocol support.
  72.     //------------------------------------------------------------------------------------
  73.  
  74.     virtual void ReadFrom(TStream* aStream);
  75.  
  76.     virtual void WriteTo(TStream* aStream);
  77.  
  78.  
  79.     //------------------------------------------------------------------------------------
  80.     // Chain Management Methods
  81.     //------------------------------------------------------------------------------------
  82.  
  83.     inline void SetNextBehavior(TBehavior* nextBehavior)
  84.     { fNextBehavior = nextBehavior; }
  85.         // Set the fNextBehavior field
  86.  
  87.     inline void SetPreviousBehavior(TBehavior* previousBehavior)
  88.     { fPreviousBehavior = previousBehavior; }
  89.         // Set the fPreviousBehavior field
  90.  
  91.     inline TBehavior* GetNextBehavior() const
  92.     { return fNextBehavior; }
  93.         // Return the next behavior    
  94.  
  95.     TBehavior* GetNextEnabledBehavior() const;
  96.         // Return the next enabled behavior    
  97.  
  98.     virtual void SetOwner(TEventHandler* itsOwner);
  99.         // Set the fOwner field
  100.  
  101.     Boolean IsOwnerSelected() const
  102.     { return fIsOwnerSelected; }
  103.         // Returns the current state of the 'fIsOwnerSelected' flag 
  104.  
  105.     virtual void SelectOwner (Boolean select);
  106.         // Called when this behavior's owner is selected. Sets the fIsOwnerSelected flag
  107.  
  108.     inline Boolean IsEnabled() const
  109.     { return fEnabled; }
  110.         // Returns the current state of the 'fEnabled' flag 
  111.         
  112.     virtual void SetEnabled(Boolean enabled);
  113.         // Sets the 'fEnabled' flag.
  114.  
  115.     void InsertBefore(TBehavior* insertMe,
  116.                                      TBehavior* before);
  117.         // Insert behavior 'insertMe' ahead of behavior 'before' in the chain 
  118.  
  119.     void InsertAfter(TBehavior* insertMe,
  120.                                     TBehavior* after);
  121.         // Insert behavior 'insertMe' after of behavior 'after' in the chain 
  122.  
  123.     void AppendBehavior(TBehavior* aBehavior);
  124.         // Add 'aBehavior' to the end of the list 
  125.  
  126.     void RemoveBehavior(TBehavior* removeMe);
  127.         // Remove behavior 'removeMe' from the chain 
  128.  
  129.     //------------------------------------------------------------------------------------
  130.     // • Event Filtering Methods
  131.     //------------------------------------------------------------------------------------
  132.  
  133.     virtual void DoCommandKeyEvent(TToolboxEvent* event);
  134.         // Allows behaviors to respond to CommandKey events, if there is a next behavior
  135.         // it passes the message on down the chain otherwise it calls the corresponding
  136.         // method of its Owner 
  137.  
  138.     virtual void DoEvent(EventNumber eventNumber,
  139.                                 TEventHandler* source,
  140.                                 TEvent* event);
  141.         // Allows behaviors to handle events, if there is a next behavior it passes the
  142.         // message on down the chain otherwise it calls the corresponding method of its
  143.         // Owner 
  144.  
  145.     virtual void DoKeyEvent(TToolboxEvent* event);
  146.         // Allows behaviors to respond to KeyCommands, if there is a next behavior it
  147.         // passes the message on down the chain otherwise it calls the corresponding
  148.         // method of its Owner 
  149.  
  150.     virtual void DoKeyUp(TToolboxEvent* event);
  151.         // Allows behaviors to respond to KeyUps, if there is a next behavior it passes
  152.         // the message on down the chain otherwise it calls the corresponding method of
  153.         // its Owner 
  154.  
  155.     virtual void DoMenuCommand(CommandNumber aCommandNumber);
  156.         // Allows behaviors to handle menu events, if there is a next behavior passes the
  157.         // message on down the chain otherwise it calls the corresponding method of its
  158.         // Owner 
  159.  
  160.     virtual Boolean DoScriptCommand(CommandNumber        aCommandNumber,
  161.                                     TAppleEvent*        message,
  162.                                     TAppleEvent*        reply);
  163.         // Allows behaviors to handle appleevents. Returns true if the event is handled,
  164.         // and false if it was not.
  165.         
  166.     virtual void DoSetupMenus();
  167.         // Allows behaviors to handle menu setup if there is a next behavior passes the
  168.         // message on down the chain otherwise it calls the corresponding method of its
  169.         // Owner 
  170.  
  171.     virtual void DoBehaviorUpdate(ChangeID theChange,
  172.                                          TObject* changedObject,
  173.                                          TObject* changedBy,
  174.                                          TDependencySpace* dependencySpace);
  175.         // Allows behaviors to handle updates from notifiers 
  176.  
  177.     // • Special hooks which are used when attached to a TView
  178.  
  179.     virtual Boolean DoMouseCommand(VPoint& theMouse,
  180.                                           TToolboxEvent* event,
  181.                                           CPoint hysteresis);
  182.         // Allow view behaviors to intercept mouse commands, if there is a next behavior
  183.         // it passes the message on down the chain otherwise by default returns false 
  184.         
  185.     virtual Boolean DoMouseUp(VPoint& theMouse,
  186.                                        TToolboxEvent* event,
  187.                                        CPoint hysteresis);
  188.         // Allow view behaviors to intercept mouse up commands.
  189.  
  190.     virtual void Draw ( const VRect& area );
  191.         // Called during a views drawing process, after the view has been called to draw
  192.         // itself and before the views subviews are called to draw themselves. The 'area'
  193.         // in need of drawing is passed in. 
  194.         
  195.     virtual Boolean DoSetCursor(const VPoint& localPoint, RgnHandle cursorRegion);
  196.         // Gives the view behavior the opportunity to setup the cursor, if there is a nex
  197.         // behavior it passes the message on down the chain otherwise by default returns
  198.         // false 
  199.  
  200.     // • Special hooks which are used when attached to a TApplication
  201.  
  202.     virtual Boolean DoToolboxEvent(TToolboxEvent* event);
  203.         // When this is called by MacApp, theEvent is the event to be processed.
  204.         
  205.     virtual void DoPostCreate(TDocument* itsDocument);
  206.         // Gives the behavior the opportunity to do any processing after its
  207.         // associated view has been fully created
  208.  
  209.     //------------------------------------------------------------------------------------
  210.     // data members
  211.     //------------------------------------------------------------------------------------
  212. public:
  213.     TEventHandler* fOwner;                    // The event handler that owns this behavior
  214.  
  215.     TBehavior* fNextBehavior;                //    The next behavior in the chain
  216.  
  217.     TBehavior* fPreviousBehavior;            //    The previous behavior in the chain
  218.  
  219.     IDType fIdentifier;
  220.  
  221.     long fIdleFreq;                            // the minimum number of ticks that can pass
  222.                                             // before my DoIdle is called. 0 = call as
  223.                                             // often as possible. n..kMaxIdleTime = call
  224.                                             // every fIdleFreq ticks.
  225.                                                 
  226.     long fLastIdle;                            // the tick count the last time my DoIdle was
  227.                                             // called.
  228.  
  229.     Boolean        fEnabled;                    // True if this behavior is enabled. If false,
  230.                                             // this behavior will be skipped over as
  231.                                             // though it were not in the chain.
  232.  
  233.     Boolean        fIsOwnerSelected;            // True if this behavior's owner is "selected"
  234.                                             // as in a prototyping environment. Treat this
  235.                                             // instance variable as private, and call
  236.                                             // TEventHandler::SetSelect
  237.  
  238.  
  239. };
  240.  
  241.  
  242. //----------------------------------------------------------------------------------------
  243. // CBehaviorIterator: A simple iterator for the behavior chain.
  244. //----------------------------------------------------------------------------------------
  245.  
  246. class CBehaviorIterator
  247. {
  248. public:
  249.     CBehaviorIterator(TEventHandler* itsEventHandler);
  250.     
  251.     inline Boolean More()                    // override
  252.     { return (fCurrentBehavior != NULL); }
  253.         // Returns true if there are more elements to iterate over
  254.  
  255.     inline void Reset()                    // override
  256.     { fCurrentBehavior = fFirstBehavior; }
  257.         // Resets the iterator to begin again
  258.     
  259.     inline TBehavior* CurrentBehavior()
  260.     { return fCurrentBehavior; }
  261.         // returns the current Behavior in the Behavior chain
  262.  
  263.     inline TBehavior* FirstBehavior()
  264.     { Reset(); return fFirstBehavior; }
  265.         // Resets the iterator to begin again and returns the first Behavior in the
  266.         // Behavior chain
  267.  
  268.     inline TBehavior* NextBehavior()
  269.     { Advance(); return fCurrentBehavior; }
  270.         // returns the next Behavior in the Behavior chain
  271.     
  272. protected:
  273.     void Advance();                    // override
  274.         // Advances the iteration
  275.  
  276. private:
  277.     TBehavior*    fFirstBehavior;
  278.  
  279.     TBehavior*    fCurrentBehavior;
  280.  
  281.     TBehavior*    fNextBehavior;
  282. };
  283.  
  284. #endif
  285.